package com.zhouyu.advisor;

import com.zhouyu.advice.ZhouyuBeforeAdvice;
import com.zhouyu.service.UserService;
import org.aopalliance.aop.Advice;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.MethodMatcher;
import org.springframework.aop.Pointcut;
import org.springframework.aop.PointcutAdvisor;
import org.springframework.aop.support.StaticMethodMatcherPointcut;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @author leimingchao
 * create 2025-05-16 20:33
 */
@Component
public class ZhouyuPointcutAdvisor implements PointcutAdvisor {
	@Override
	public Advice getAdvice() {
		return new ZhouyuBeforeAdvice();
	}

	@Override
	public boolean isPerInstance() {
		return false;
	}

	//@Override
	//public Pointcut getPointcut() {
	//	return new StaticMethodMatcherPointcut() {
	//		@Override
	//		public boolean matches(Method method, Class<?> targetClass) {
	//			return "test".equals(method.getName());
	//		}
	//	};
	//}

	@Override
	public Pointcut getPointcut() {
		return new Pointcut() {
			@Override
			public ClassFilter getClassFilter() {
				return new ClassFilter() {
					/**
					 *
					 * @param clazz the candidate target class 被代理类
					 * @return
					 */
					@Override
					public boolean matches(Class<?> clazz) {
						return UserService.class.equals(clazz);
					}
				};
			}

			@Override
			public MethodMatcher getMethodMatcher() {
				return new MethodMatcher() {
					@Override
					public boolean matches(Method method, Class<?> targetClass) {
						return false;
					}

					@Override
					public boolean isRuntime() {
						return true;
					}

					@Override
					public boolean matches(Method method, Class<?> targetClass, Object... args) {
						return false;
					}
				};
			}
		};
	}
}
